单击时获取2 x getElementById元素

时间:2014-03-13 22:09:40

标签: javascript jquery

我必须修改以下代码以包含persDonateAllocat的值:

<script type="text/javascript">
    function doAddPersonalDonation(personaldonation, id){
            if(isNaN(personaldonation)){
                alert("Please enter only round numbers and no cents."); 
            }else{
                window.location = "adopt.php?action=add&personaldonation=" + personaldonation + "&id=" + id + "&all=" + persDonateAllocat;
            }
        }
    </script>

<a href=\"javascript:doAddPersonalDonation(
            document.getElementById('personaldonation').value,$id
            )\">Add To Cart</a>

我已经尝试了以下代码,但点击链接现在什么也没做:

<a href=\"javascript:doAddPersonalDonation(personaldonation, persDonateAllocat){
            document.getElementById('personaldonation').value,$id;
            document.getElementById('persDonateAllocat').value;
            } \">Add To Cart</a>

我添加了这个,它现在完美无缺:

<script type="text/javascript">
    function doAddPersonalDonation(personaldonation, id){
        var persDonateAllocat= document.getElementById('persDonateAllocat').value;
            if(isNaN(personaldonation)){
                alert("Please enter only round numbers and no cents."); 
            }else{
                window.location = "adoptions_2.php?action=add&personaldonation=" + personaldonation + "&id=" + id + "&all=" + persDonateAllocat;
            }
        }
    </script>

<a href=\"javascript:doAddPersonalDonation(
                document.getElementById('personaldonation').value,$id
                )
                \">Add To Cart</a>

1 个答案:

答案 0 :(得分:1)

这个怎么样?的 DEMO

<强> HTML:

<a id="myLink">Some link</a>
<input id="personaldonation" />
<input id="persDonateAllocat" />

<强> jQuery的:

$("#myLink").click(function() {
    var id = 123;
    var personaldonation = $("#personaldonation").val();
    var persDonateAllocat = $("#persDonateAllocat").val();
    window.location = "adopt.php?action=add&personaldonation=" + personaldonation + "&id=" + id + "&all=" + persDonateAllocat;
});