jQuery读取非空和可见的文本字段

时间:2022-03-10 22:46:04

I am building up a multi-step questionnaire. I have 3 questions (3 divs), the DOM looks like this(Pseudo-code). My question are

我正在建立一个多步骤问卷。我有3个问题(3个div), DOM看起来是这样的(伪代码)。我的问题是

1.How can I read the value from the field which type is url ( type='url') in q3?

1。如何从q3中的url类型(type='url')中读取值?

2.What do read only the non-empty text/textarea/url fields? Meaning I only want to read the text fields when user typed something into it.

2。什么只读取非空的文本/textarea/url字段?也就是说,我只想在用户输入内容时读取文本字段。

I was thinking a stupid way to read each fields no matter if it empty. then i have isset/empty php command to see if this is empty, if so, then i will not take a value. But is there any better way to achieve this?

我在用一种愚蠢的方式去阅读每一个领域,不管它是不是空的。然后我有isset/empty php命令,看看这个是否为空,如果为空,那么我将不取值。但有没有更好的方法来实现这一点呢?

 <div id=q1>
   <input type='text' id='q1text'>
   <input type='button'>   // this btn will hide q1 and show q2.
 </div>

 <div id=q2 style="display:none">
   <input type='textarea' id='q2textarea'>
   <input type='button'>  // this btn will hide q2 and show q3
 </div> 

 <div id=q3 style="display:none">
   <input type='url' id='q3url'>    // this btn will submit the form data.
   <input type='submit'>
 </div>

1 个解决方案

#1


6  

1.How can I read the value from the field which type is url ( type='url') in q3?

1。如何从q3中的url类型(type='url')中读取值?

It has id attribute. You can use id selector along with .val()

id属性。可以使用id选择器和.val()

$('#q3url').val();

2.What do read only the non-empty text/textarea/url fields? Meaning I only want to read the text fields when user typed something into it.

2。什么只读取非空的文本/textarea/url字段?也就是说,我只想在用户输入内容时读取文本字段。

You can use filter function to filter out the element that do not have value in them:

您可以使用filter函数来过滤掉那些没有值的元素:

var allnonemptyurls = $('input[type="url"]').filter(function () {
  return !!this.value;
})

#1


6  

1.How can I read the value from the field which type is url ( type='url') in q3?

1。如何从q3中的url类型(type='url')中读取值?

It has id attribute. You can use id selector along with .val()

id属性。可以使用id选择器和.val()

$('#q3url').val();

2.What do read only the non-empty text/textarea/url fields? Meaning I only want to read the text fields when user typed something into it.

2。什么只读取非空的文本/textarea/url字段?也就是说,我只想在用户输入内容时读取文本字段。

You can use filter function to filter out the element that do not have value in them:

您可以使用filter函数来过滤掉那些没有值的元素:

var allnonemptyurls = $('input[type="url"]').filter(function () {
  return !!this.value;
})