用于填充文本字段的JS脚本

时间:2017-06-16 18:23:40

标签: javascript html

我有一个我正在处理的入学申请表,我正在尝试根据是否选中某些单选按钮来填充表单输入(根据表单中的答案填充电子邮件主题)。我在<head>底部有以下代码,并且在<script type="text/javascript"> if (form.id1.checked == true) { document.getElementById("EMAIL_TITLE").value = "subject line"; } else if (form.id2.checked == true) { document.getElementById("EMAIL_TITLE").value = "subject line"; } else if (form.id3.checked == true) { document.getElementById("EMAIL_TITLE").value = "subject line"; } else if (form.id4.checked == true) { document.getElementById("EMAIL_TITLE").value = "subject line"; } else if (form.id5.checked == true) { document.getElementById("EMAIL_TITLE").value = "subject line"; } else { document.getElementById("EMAIL_TITLE").value = "unknown"; } </script> 中也尝试过,但没有成功。关于为什么这似乎不起作用的任何想法?

<script type="text/javascript">
function handleChange()
{
    var value = document.form.Request_Type.value;
        if (value == "Request_Access") {
            document.getElementById("EMAIL_TITLE").value = "subject line 1";
        }

        else if (value == "Request_Support") {
            document.getElementById("EMAIL_TITLE").value = "subject line 2";
        }

        else if (value == "Request_Reporting") {
            document.getElementById("EMAIL_TITLE").value = "subject line 3";
        }           

        else if (value == "Request_Retire") {
            document.getElementById("EMAIL_TITLE").value = "subject line 4";
        }   

        else if (value == "Request_Other") {
            document.getElementById("EMAIL_TITLE").value = "subject line 5";
        }

        else {
        document.getElementById("EMAIL_TITLE").value = "subject line - Unknown";
        }
}       
</script>

<input type="radio" name="Request_Type" onchange="handleChange();" value="Request_Access" id="access">
<input type="radio" name="Request_Type" onchange="handleChange();" value="Request_Support" id="support">
<input type="radio" name="Request_Type" onchange="handleChange();" value="Request_Reporting" id="reporting">
<input type="radio" name="Request_Type" onchange="handleChange();" value="Request_Retire" id="retire">
<input type="radio" name="Request_Type" onchange="handleChange();" value="Request_Other" id="other">

根据@mhatch的回答,我已将我的代码更新为以下内容,但仍然存在问题。我现在可能遗漏了一些简单/基本的东西,但是我很难理解

.setOption('vAxes', {
  vAxes:{
    0: y1parameters,

    1: y2parameters
  },
  series:{
    0: {
      targetAxisIndex: 0
    },
    1: {
      targetAxisIndex: 1
    },
   },
})

1 个答案:

答案 0 :(得分:0)

首先,如果它们是单选按钮,则需要具有相同的name属性。代码form.id1.checked表明每个按钮都有自己的名称值。这将导致所有按钮同时可点击。如果这是你想要的,你应该使用复选框。

其次,您需要将您的逻辑附加到事件中。

我想你正在寻找这样的东西:

function handleChange(){
	var value = document.form.id.value;
	if (value == "1") {
        document.getElementById("EMAIL_TITLE").value = "subject line1";
    }

    else if (value == "2") {
        document.getElementById("EMAIL_TITLE").value = "subject line2";
    }

    else if (value == "3") {
        document.getElementById("EMAIL_TITLE").value = "subject line3";
    }           

    else if (value == "4") {
        document.getElementById("EMAIL_TITLE").value = "subject line4";
    }   

    else if (value == "5") {
        document.getElementById("EMAIL_TITLE").value = "subject line5";
    }

    else {
    document.getElementById("EMAIL_TITLE").value = "unknown";
    }
}
<form action="" name="form">
	<input type="radio" name="id" onchange="handleChange();" value="1" />
	<input type="radio" name="id" onchange="handleChange();" value="2" />
	<input type="radio" name="id" onchange="handleChange();" value="3" />
	<input type="radio" name="id" onchange="handleChange();" value="4" />
	<input type="radio" name="id" onchange="handleChange();" value="5" />
	<input type="text" name="title" id="EMAIL_TITLE" value="" />
</form>