我正在嘗試根據表單中選擇的輸入更新 url 引數。
<form method="get" action="current.php" id="header-form">
<input type="hidden" name="location" value="location">
<input type="checkbox" name="type[]" class="type" value="opt1"> option1
<input type="checkbox" name="type[]" class="type" value="opt2"> option2
<input type="checkbox" name="type2[]" class="type1" value="new1"> new1
<input type="checkbox" name="type2[]" class="type1" value="new2"> new2
</form>
查詢
通過選擇復選框,我想將值附加到 URL
我的jQuery
<script type="text/javascript">
$(document).ready(function() {
$(".type").change(function() {
$("#header-form").trigger("submit");
});
$(".type1").change(function() {
$("#header-form").trigger("submit");
});
});
</script>
如果我選擇選項 1,它作業正常,如果我嘗試選擇選項 2,它會從 URL 中洗掉選項 1。
如果我選擇選項 1 和選項 2
URL 應該是 -> http://localhost.current.php?type=option1&option2
如果我選擇所有值 option1 & option 2 和 new 1 & new 2
URL 應該是 -> http://localhost.current.php?type=option1&option2?type1=new1&new2
如果我選擇值 option1 & new 1 & new 2
URL 應該是 -> http://localhost.current.php?type=option1?type1=new1&new2
uj5u.com熱心網友回復:
您必須保留之前提交的資料。
用 PHP 做到這一點:
<form method="get" action="current.php" id="header-form">
<input type="hidden" name="location" value="location">
<input type="checkbox" name="type[]" <?php if(isset($_GET['type']) && in_array('opt1', $_GET['type'])): ?>checked<?php endif; ?> class="type" value="opt1"> option1
<input type="checkbox" name="type[]" <?php if(isset($_GET['type']) && in_array('opt2', $_GET['type'])): ?>checked<?php endif; ?> class="type" value="opt2"> option2
<input type="checkbox" name="type2[]" <?php if(isset($_GET['type2']) && in_array('new1', $_GET['type2'])): ?>checked<?php endif; ?> class="type1" value="new1"> new1
<input type="checkbox" name="type2[]" <?php if(isset($_GET['type2']) && in_array('new2', $_GET['type2'])): ?>checked<?php endif; ?> class="type1" value="new2"> new2
</form>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/350014.html
標籤:javascript php html 查询
