JavaScript-将循环变量包含在HTML值中'' "属性

时间:2017-01-23 19:35:20

标签: javascript html drop-down-menu

我正在尝试下一个"分钟"使用 private static void SelectContentsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is TextBox) { TextBox textBox = d as TextBox; if ((e.NewValue as bool?).GetValueOrDefault(false)) { textBox.GotKeyboardFocus += OnKeyboardFocusSelectText; } else { textBox.GotKeyboardFocus -= OnKeyboardFocusSelectText; } } } private static void OnKeyboardFocusSelectText(object sender, KeyboardFocusChangedEventArgs e) { if (e.KeyboardDevice.IsKeyDown(Key.Tab)) ((TextBox)sender).SelectAll(); } 循环的字段。我可以在下拉列表中显示值。我无法做的是将for属性包含在下拉列表中。我的value=" "与我使用value循环创建的数字相同。

for

那么,我如何获得<select id="autoDropDown"> <script> var minute=0; for (var i=0; i <=58; i++) { minute++; document.write("<option value=\"minute\">" + minute + "</option>"); } </script> </select> 循环包含的value属性。我也想&#34;得到&#34;选定的值,并在JavaScript中以for显示。

4 个答案:

答案 0 :(得分:0)

 <select id="autoDropDown">
        <script>
            var minute=0;
            for (var i=0; i <=58; i++)
            {
                minute++;
                document.write("<option value=\""+minute+"\">" + minute + "</option>");

            }

        </script>
    </select>

答案 1 :(得分:0)

您可以组合单引号和双引号。只需在字符串中使用单引号:

public void SwapVideo(int index){

    videoSphere.GetComponentInChildren<GvrVideoPlayerTexture> ().videoURL = urls [index];// my new url 
    videoSphere.GetComponentInChildren<GvrVideoPlayerTexture>().ReInitializeVideo ();
    videoSphere.SetActive (true);

}

public void ReturnToMainMenu(){

    videoSphere.GetComponentInChildren<GvrVideoPlayerTexture>().CleanupVideo();
    videoSphere.SetActive (false);

    this.gameObject.SetActive (true);

}

为什么要使用辅助变量?

"<option value='" + minute + "' >" + minute + "</option>"

至于获得它:

<select id="autoDropDown">
    <script>
        for (var i = 1; i <= 59; i++)
        {
            document.write("<option value='" + i + "'>" + i + "</option>");
        }
    </script>
</select>

答案 2 :(得分:0)

您可以使用onchange方法获取值,如下所示:

<select id="autoDropDown" onchange="someFunction()">

<select id="autoDropDown" onchange="alert(this.value)">
每次yoy选择不同的值时,

获得te值。

用于设置值:

document.write("<option value="+ minute +">" + minute + "</option>");

答案 3 :(得分:0)

与“Chris G”相同,几乎没有修改

<select id="autoDropDown">
    <script>
        for (var i=0; i <60; i++)
        {
            document.write("<option value='"+ i +"'>" + ("0" + i).slice(-2) + "</option>");
        }
    </script>
</select>
<button onclick="alert(parseInt(document.getElementById('autoDropDown').value));"> Get Value </button>
<button onclick="alert(Number(document.getElementById('autoDropDown').value));"> Get Value </button>

输出:

enter image description here