簡體   English   中英

如何使用ajax或javascript將復選框值傳遞給另一個頁面

[英]How to pass checkbox values to another page using ajax or javascript

我使用下面的復選框類型。 我想在另一頁中使用所選項目值。 我正在使用javascript和ajax。

我想使用ajax將[url]選中的復選框值傳遞給另一個頁面..

請幫我!

<form>
    <input type="checkbox" name="vehicle" value="Bike">I have a bike
    <br>
    <input type="checkbox" name="vehicle" value="Car">I have a car
    <br>
    <input type="checkbox" name="vehicle" value="cycle">I have a cycle
    <br>
    <input type="checkbox" name="vehicle" value="lorry">I have a lorry
    <br>
    <input type="checkbox" name="vehicle" value="house">I have a house
    <br>
    <input type="checkbox" name="vehicle" value="mobile">I have a mobile
    <br>
    <input type="button" value="what do u have" onClick=function();/>
</form>

提前致謝!

我建議你為每個元素添加一個ID。 然后只需為每個文件執行document.getElementbyId,檢查是否選中了復選框,如果是,則將它們的值添加到下面的“data”數組中。 (例如,value1:document.getElementById(“cycle”)。value)。 這將通過post發送一個你可以在php函數中使用的變量($ _POST ['value1'])

請注意,您需要jquery才能使用以下語法。 此外,如果您需要對php函數返回的值執行某些操作,則可以使用下面發布的succes函數。

function send()
 {

   $.ajax({ url: 'yourfile.php',
             data: {value1: value1, value2: value2...}, //Modify this
             type: 'post',
             success:   function(output) {

                         alert("It worked!"); 
                 var result= eval("(function(){return " + output + ";})()"); 

             }
   });
}

如果你的元素非常少......也就是你在問題中提到的那樣你可以編寫自己的代碼來獲取值並使用javascript或jquery將其傳遞給服務器端。

使用jquery ajax方法將數據發布到服務器。

您也可以使用jquery表單提交完整的表單。 http://www.malsup.com/jquery/form/

對於初學者,我會說你將復選框的值添加到變量中。 這個js獲得了你的價值。 如果要使用它發送更多信息,請使用“this”並訪問函數中的屬性。 如果您想將其發送到另一頁,我建議您查看POST方法。 有關stackoverflow的幾個問題已經存在。 喜歡這個Posting(AJAX)數據到另一個頁面

<html>
<script>
function getvalue(checkbox)
{
    alert(checkbox);
}

</script>

<form> 
<input type="checkbox" onclick="getvalue(this.value)" name="vehicle" value="Bike">I have a bike<br> 
<input type="checkbox" name="vehicle" value="Car">I have a car <br> 
<input type="checkbox" name="vehicle" value="cycle">I have a cycle<br> 
<input type="checkbox" name="vehicle" value="lorry">I have a lorry <br> 
<input type="checkbox" name="vehicle" value="house">I have a house<br> 
<input type="checkbox" name="vehicle" value="mobile">I have a mobile 
<br> <input type="button" value="what do u have" onClick=function();/> 
</form>
</html>

希望這有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM