js与ajax获取数据

js接受页面的数据传到后台处理并返回给前台页面

<td>
    <input type="text" name="test" id="test" value="" />
</td>
 
<script>
    $("#year").change(function(){
 
    var year = document.getElementById("year").value; 
    var ywlx = document.getElementById("ywlx").value;
    $.ajax({
        url:'',
        type:"POST",
        data:{ year:year , ywlx:ywlx },
        dataType:"json",
        success:function(aaa){
 
            $(this).val(""); //清空上次input框里的数据
            $('#test').val(aaa['boxnum']);  //往input框里传值
        }
    });
});
</script>

如果是select中的option的值,就有两种实现方式:

js如何获取到select的option值

1、获得选项option的值
var obj = document.getElementByIdx_x(”testSelect”); //定位id
var index = obj.selectedIndex; // 选中索引
var text = obj.options[index].text; // 选中文本
var value = obj.options[index].value; // 选中值

jQuery中获得选中select值

第一种方式
$('#testSelect option:selected').text();//选中的文本
$('#testSelect option:selected') .val();//选中的值
$("#testSelect ").get(0).selectedIndex;//索引

第二种方式
$("#tesetSelect").find("option:selected").text();//选中的文本
…….val();
…….get(0).selectedIndex;

猜你喜欢

转载自blog.csdn.net/jianshou6442/article/details/82699205