为什么$(“#id”)。val()不是函数

时间:2016-11-24 01:30:25

标签: javascript jquery ajax

jsp:

<input id="companyName" class="cpn" type="text" value="" ></input> 
<input id="phoneNumber" class="number" type="text" value=""></input>    
<textarea id="remark" placeholder="消息备注"></textarea>
<input  class="submit" type="button" onclick="apply()" value="在线提交" ></input>//define some block<input>

js:

var companyName = $("#companyName").val();//get the value in block<input>
var phoneNumber = $("#phoneNumber").val();
var remark = $('#remark').val();

firefox和google节目.val()不是函数,所以我更改了代码:

var companyName = document.getElementById("companyName").value;
alert(companyName);// not use the jquery
var phoneNumber = document.getElementById("phoneNumber").value;
alert(phoneNumber);

现在它获得了vaule,但是ajax代码仍然无法工作: 谁能告诉我?

1 个答案:

答案 0 :(得分:0)

# Please try bellow code in javascript #    

 <script>
        function apply(){
            var companyName = document.getElementById("companyName").value;
            alert(companyName);

            var phoneNumber = document.getElementById("phoneNumber").value;
            alert(phoneNumber);

            var remark = document.getElementById("remark").value;
            alert(remark);
        }
    </script>
    <input id="companyName" class="cpn" type="text" value="" ></input> 
    <input id="phoneNumber" class="number" type="text" value=""></input>    
    <textarea id="remark" placeholder="enter"></textarea>
    <button class="submit" onclick="apply();">Submit</button>



# Please try bellow code in Jquery#  


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>

    function apply(){
        var companyName = $("#companyName").val();
        alert(companyName);
        var phoneNumber = $("#phoneNumber").val();
        alert(phoneNumber);
        var remark = $('#remark').val();
        alert(remark);
    }
</script>
<input id="companyName" class="cpn" type="text" value="" ></input> 
<input id="phoneNumber" class="number" type="text" value=""></input>    
<textarea id="remark" placeholder="enter"></textarea>
<button class="submit" onclick="apply();">Submit</button>