如何将选定的问题传递给文本框? [英] How to pass selected questions to textbox?

查看:104
本文介绍了如何将选定的问题传递给文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function  copy_data(val){
if document .getElementById(question1).value == ' '){
var a = document .getElementById(val.id) .value
document .getElementById( question1 )。value = a
}
var quest2 = document .getElementById(问题2)。价值;
if (quest2.value == ' '){

document .getElementById( question2)。value = a
}
var quest3 = 文档 .getElementById(问题3)。价值;
if (quest3.value == ' '){
document .getElementById( question3)。value = a
}
}



 <   input    类型  =  text   名称  =  q3    id   =  q3    value   = 你的昵称是什么?    style   =  font-size:9px    height   =  10    onclick   =  copy_data(this)   /  >  

< 输入 类型 = text 名称 = q6 id = q6 value = 你多大了? style = font-size:9px; height = 10 onclick = copy_data(this) / >

< 输入 <跨度cl ass =code-attribute>类型 = text 名称 = q4 id = q4 value = 您的出生日期是什么? 样式 = font-size:9px height = 10 onclick = copy_data(this) / > ;



< input id = question1 name = question1 type = text / >
< 输入 id = question2 name = question2 type < span class =code-keyword> = text / >
< 输入 id = question3 名称 = question3 type = text / >





在我的代码中点击每个问题文本移动到特定文本框。

如果question1不是空文本将其移动到question2文本框。





帮助任何人



谢谢..

解决方案

您目前的代码存在一些问题:



  1. 有时候没有引用 getElementById 中的ID。
  2. quest2 quest3 已包含 question2 question3 元素的值,因此您无法获得 .value 第二次来自它。
  3. 如果您解决了上述问题,仍然存在这个问题:单击一个框将填充所有空文本框同样的问题。这是不可取的,因此您应首先检查 val.id 以查看应更改的文本框。



此代码段应解决上述问题:

 function copy_data(val){
var quest1 = document.getElementById( question1);
var quest2 = document.getElementById( 问题2\" );
var quest3 = document.getElementById( 问题3\" );
if (quest1。 value == ' '&& val.id == ' q3'){
quest1。 value = val。 value ;
} else if (quest2。 value == ' '&& val.id == ' q6'){
quest2。 value = val 。 value ;
} else if (quest3。 value == ' '&& val.id == ' q4'){
quest3。 value = val 。 value ;
}
}


希望下面的片段有用



< pre lang =Javascript> function copy_data(val)
{
var a = document .getElementById(val.id).value;
var quest1 = document .getElementById( question1)。value;
var quest2 = document .getElementById( question2)。value;
var quest3 = document .getElementById( question3)。value;
if (quest1 ==
{
document .getElementById( 问题1\" )值= A;
}
如果(quest2 ==
{
document .getElementById( question2)。value = a;
}
如果(quest3 ==
{
document .getElementById( question3)。value = a;
}
}


您好b $ b

请使用jquery找到解决方案< br $>
1.从jquery网站下载min.js文件,添加对项目的引用。





 <  !DOCTYPE     html  >  

< html lang = zh xmlns = http://www.w3.org/1999/xhtml >
< >
< meta charset = utf-8 / >
< script src = Scripts / jquery-1.8.2.js > < / script >
< < span class =code-leadattribute> script
type = text / javascript >

function copy_data(val){
if(document.getElementById(question1).value == ''){
 var a = document.getElementById(val.id).value
 document.getElementById("question1").value=a
 }
 var quest2 =document.getElementById(question2).value;
 if(quest2.value == ''){

 document.getElementById("question2").value=a
 }
 var quest3 = document.getElementById(question3).value;
 if(quest3.value == ''){
 document.getElementById("question3").value=a
 }
 }


<input type="text" name="q3" id="q3" value="What is your nick name?" style="font-size:9px" height="10" onclick="copy_data(this)"/>
 
<input type="text" name="q6" id="q6" value="How old are you?" style="font-size:9px;" height="10" onclick="copy_data(this)"/>
 
<input type="text" name="q4" id="q4" value="What is your birth date?" style="font-size:9px" height="10" onclick="copy_data(this)"/>
 


<input id="question1" name="question1" type="text"/>
 <input id="question2" name="question2" type="text"/>
 <input id="question3" name="question3" type="text"/>



In my code by clicking each question text move into the particular textbox.
if question1 is not empty text move it to question2 textbox .


help anyone

thank you..

解决方案

There are some problems with the code you currently have:


  1. Sometimes there are no quotes around the IDs inside getElementById.
  2. quest2 and quest3 already contain the values of the question2 and question3 elements, so you cannot get .value from it a second time.
  3. If you fix the problems above, there's still this problem remaining: clicking on one box will fill all empty text boxes with the same question. This is undesirable, so you should first check val.id to see which text box you should change.


This code snippet should fix the above problems:

function copy_data(val) {
    var quest1 = document.getElementById("question1");
    var quest2 = document.getElementById("question2");
    var quest3 = document.getElementById("question3");
    if (quest1.value == '' && val.id == 'q3') {
        quest1.value = val.value;
    } else if (quest2.value == '' && val.id == 'q6') {
        quest2.value = val.value;
    } else if (quest3.value == '' && val.id == 'q4') {
        quest3.value = val.value;
    }
}


Hope the below snippet helps

function copy_data(val)
{
	var a = document.getElementById(val.id).value;	
	var quest1 = document.getElementById("question1").value;
	var quest2 = document.getElementById("question2").value;
	var quest3 = document.getElementById("question3").value;
	if(quest1 == "")
	{
		document.getElementById("question1").value=a;
	}
	if (quest2 == "")
	{
		document.getElementById("question2").value=a;	
	}
	if (quest3 == "")
	{
		document.getElementById("question3").value=a;	
	}
}


Hi
Please find the solutions for this using jquery
1. download min.js file from jquery website add reference to your project.


<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <script src="Scripts/jquery-1.8.2.js"></script>
    <script type="text/javascript">


这篇关于如何将选定的问题传递给文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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