在Html中获取列表中的选定值

时间:2011-03-11 10:18:41

标签: javascript jquery html jquery-mobile

我想在列表中获取所选值。

<html >
<head>
<meta charset="utf-8" />
<title>Intro to jQuery Mobile</title><br />
<!--
<script type="text/javascript" src="jquery.mobile-1.0a3.min.css"></script>
<script type="text/javascript" src="jquery.mobile-1.0a3.min.js"></script>
<script type="text/javascript" src="jquery-1.5.min.js"></script>-->
<link rel="stylesheet" href="jquery.mobile-1.0a3.min.css" />
<script type="text/javascript" src="jquery-1.5.min.js"></script>
<script type="text/javascript" src="jquery.mobile-1.0a3.min.js"></script>
<script type="text/javascript">
 function GetSelectedValut()
{
    var list_value = document.getElementById("search1").value;
    alert(list_value);
}
</script>
</head>
<body>
<!-- Page one starts-->
<div data-role="page" id="pageone">
    <div data-role="header">
        <h1>Header</h1>
    </div>
    <div data-role="content">
        <ul data-role="listview" data-inset="true" >
        <li id="search">
<a href="#pagetwo" value="Selected one" onclick="GetSelectedValut()">Selected one</a>
        </li>
        <li id="search1" onclick="GetSelectedValut()" value="Selected two">
            <a href="#detailpage">Selected two</a>
        </li>
        <li>
            <a href="#detailpage">Selected three</a>
        </li>
        <li>
        <a href="#detailpage">Selected four</a>
        </li>
        <li>
            <a href="#detailpage">Selected five</a>
        </li>
        <li>
            <a href="#detailpage">Selected six</a>
        </li>
        /ul>
    </div>
    <div data-role="footer">
        <h1>Footer</h1>
    </div>
</div>
<!-- Page one ends-->
</body>
</html>

如果我选择了六个选项,一个警告框会显示所选列表值,我也希望绑定一个标签,即下一页中的<p>段。提前谢谢。

1 个答案:

答案 0 :(得分:2)

function GetSelectedValut(id)
{
    var list_value = $("#"+id).attr("value");
    location = "nextpage.html?val="+encodeURIComponent(list_value);
}


    <a href="#pagetwo" value="Selected one" onclick="GetSelectedValut('search1')">Selected one</a>
    </li>
    <li id="search1" onclick="GetSelectedValut(this.id)" value="Selected two">

在下一页(来自this post):

function getParameterByName(name) { 
  var match = RegExp('[?&]' + name + '=([^&]*)')
    .exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

window.onload=function() {
  $("#someParagraphID").html(getParameterByName("val"))
}