如何使用jquery或js选择下拉选项..? [英] How to select dropdown option using jquery or js ..?

查看:69
本文介绍了如何使用jquery或js选择下拉选项..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <  !DOCTYPE     html  >  
< html xmlns = http://www.w3.org/1999/xhtml >
< head >
< title > Fred Travels < / title >
< scri pt src = jquery.min.js > < / script >
< 脚本 >
$( document )。ready( function (){ // 页面加载
$ .ajax({
type: GET
url: http:// localhost:2160 / CustomerService.svc / CustomerTypeSelect
cont entType: application / json; charset = utf-8
data:{},
dataType: json
processData: true
成功: function (数据,状态,jqXHR,结果){
if (data.length> 0 ){
var $ el = $( #ddlCustType);
$ el.empty(); // 删除旧选项
$ el.append($( < option>< / option> )。attr( value 0 ) .text(' 请选择'));
for var i = 0 ; i< data.length; i ++){
$ el.append($( < ;选项>< /选项>)。attr( value,数据[I] .CusTypeID)的.text(数据[I] .CustTypeName));
}
}
}
});
CustomerSelectviaID( 1 );
});

function CustomerSelectviaID(CustID){
var Customer = {
rowno: 1
TotalRecords: 5
CustomerID: CustID
};
$ .ajax({
type: POST
url: http:// localhost:2160 / CustomerService.svc / CustomerSelect
data: JSON .stringify(Customer),
contentType: application / json; charset = utf-8
dataType: json
processData: true
成功: function (数据,状态,jqXHR,结果){
if (data.length> 0 ){
alert( test);
$(' #ddlCustType')。val(data [ 0 ]型)。
return false ;
}
}
});
$(' #hdnCustID')。val(CustID);
}
< / 脚本 >
< / head >
< body >
< div >
< 选择 id = ddlCustType class = form-control >
< 选项 value = 0 > 请选择< /选项 >
< 选项 = 1 > 公司< / option >
< 选项 value = 2 < span class =code-keyword>> 旅游运营商< / option < span class =code-keyword>>

< 选项 value = 3 > 直接客户< / option >
< 选项 value = 4 &g t; out source < / option >
< / select >
< / div >
< / body >
< / html >





我的尝试:



我使用html选择Dropdownlist ....我加载选项值是动态的...我试图加载下拉值如

例如:$('#ddlCustType')。val(data [0] .Type);他们没有工作....同时我使用上面的代码警告(测试);那个时候他们正确加载...我不知道发生什么问题... PLZ帮助..!谢谢

解决方案

document )。ready( function (){ // 页面加载


。 ajax({
type: GET
url: http:// localhost:2160 / CustomerService.svc / CustomerTypeSelect
contentType: application / json; charset = utf-8
data:{},
dataType: json
processData: true
成功: function (数据,状态,jqXHR,结果){
if (data.length> 0 ){
var


el =

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Fred Travels</title>
    <script src="jquery.min.js"></script>
    <script>
        $(document).ready(function () { //page load
            $.ajax({
                type: "GET",
                url: "http://localhost:2160/CustomerService.svc/CustomerTypeSelect",
                contentType: "application/json; charset=utf-8",
                data: {},
                dataType: "json",
                processData: true,
                success: function (data, status, jqXHR, result) {
                    if (data.length > 0) {
                        var $el = $("#ddlCustType");
                        $el.empty(); // remove old options
                        $el.append($("<option></option>").attr("value", 0).text('Please Select'));
                        for (var i = 0; i < data.length; i++) {
                            $el.append($("<option></option>").attr("value", data[i].CusTypeID).text(data[i].CustTypeName));
                        }
                    }
                }
            });
            CustomerSelectviaID(1);
        });

        function CustomerSelectviaID(CustID) {
            var Customer = {
                rowno: 1,
                TotalRecords: 5,
                CustomerID: CustID
            };
            $.ajax({
                type: "POST",
                url: "http://localhost:2160/CustomerService.svc/CustomerSelect",
                data: JSON.stringify(Customer),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                processData: true,
                success: function (data, status, jqXHR, result) {
                    if (data.length > 0) {  
alert("test");                     
                        $('#ddlCustType').val(data[0].Type);                       
                        return false;
                    }
                }
            });
            $('#hdnCustID').val(CustID);
        }
    </script>
</head>
<body>
    <div>
        <select id="ddlCustType" class="form-control">
            <option value="0">Please Select</option>
            <option value="1">Corporate</option>
            <option value="2">Tour Operator</option>
            <option value="3">Direct Customer</option>
            <option value="4">out source</option>
        </select>
    </div>
</body>
</html>



What I have tried:

I m using html select Dropdownlist....i load the option values are dynamically ...and i m trying to load dropdown value like
Eg: $('#ddlCustType').val(data[0].Type); they not working....and the same time i m using the above code "alert("test"); " that time they correctly load...i don't know what's problem going...plz help..! thanks

解决方案

(document).ready(function () { //page load


.ajax({ type: "GET", url: "http://localhost:2160/CustomerService.svc/CustomerTypeSelect", contentType: "application/json; charset=utf-8", data: {}, dataType: "json", processData: true, success: function (data, status, jqXHR, result) { if (data.length > 0) { var


el =


这篇关于如何使用jquery或js选择下拉选项..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆