jQuery Value Help
I'm having problems getting the value of the input field.
function Send(id){
var send_to=$("#id").val();
$.post("send.php", { id:id }, function(data) { if(data==="OK"){ } else { } });
}
Below is an example of the page.
<input type=text id=4543><button onclick=Send(4543);>SEND</button>
<input type=text id=8643><button onclick=Send(8643);>SEND</button>
<input type=text id=8645><button onclick=Send(8645);>SEND&l开发者_开发问答t;/button>
<input type=text id=2421><button onclick=Send(2421);>SEND</button>
I need to get the value of the input control whose id is passed to the send method.
Thank you! :)
If I understood correctly, then:
var send_to=$("#id").val();
should be
var send_to=$("#" + id).val();
First of all your id's are invalid(not in HTML5).
Also bind the handler in document ready function
<input type=text id="txt4543"><button class="btn">SEND</button>
<input type=text id="txt8643"><button class="btn">SEND</button>
<input type=text id="txt8645"><button class="btn">SEND</button>
<input type=text id="txt2421"><button class="btn">SEND</button>
$(function(){
$(".btn").click(function(){
var txtElem = $(this).prev();
var value = txtElem.val();
$.post("send.php", { id:txtElem.attr("id") }, function(data) { if(data==="OK"){ } else { } });
});
});
精彩评论