Jquery验证非空,是表单中的URL

时间:2017-09-01 15:58:00

标签: javascript jquery html forms url-validation

我使用以下小提琴创建表单,并检查两个字段是否为空,第二个字段是否为有效的URL。 http://jsfiddle.net/nc6NW/366/

<form method=post>
     <input type="text" id='first_name'>
     <input type="url" id='second_name'>
     <input type="submit" value="Submit" id="submit" disabled>
</form>

<script>
$(':text').keyup(function() {
     if($('#first_name').val() != "" && $('#second_name').val() != "") {
           $('#submit').removeAttr('disabled');
}    else {
           $('#submit').attr('disabled', true);   
     }
});

</script>

但是,只有在添加URL

后返回修改第一个字段时才有效

3 个答案:

答案 0 :(得分:2)

您正在收听':text'密钥。因为您的第二个输入是'url'类型,所以它不会触发该函数。

这就是你必须回到第一次输入的原因。

答案 1 :(得分:2)

问题$("input").keyup(function() { if($('#first_name').val() != "" && $('#second_name').val() != "") { $('#submit').removeAttr('disabled'); } else { $('#submit').attr('disabled', true); } });

将您的jQuery更新为以下

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

num_puntos = 1000
conjunto_puntos = []
for i in xrange(num_puntos):
    x1= np.random.normal(0.0, 1.55)
    y1= np.sin(x1) * 4.1 + 0.3 + np.random.normal(0.0, 0.03)
    conjunto_puntos.append([x1, y1])
x_data = [v[0] for v in conjunto_puntos]  #x data
y_data = [v[1] for v in conjunto_puntos]  # target data (y data)
x_data2 = [v[0]**2 for v in conjunto_puntos] # x data squared
x_data3 = [v[0]**3 for v in conjunto_puntos] # x data to the power of 3

plt.plot(x_data, y_data, 'ro', label='Original data') #plot of the original data
plt.legend()
plt.show() 



W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))  #variable 1
W1 = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) # variable 2
W2 = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) #variable 3
b = tf.Variable(tf.zeros([1])) # variable 4
y = W * x_data + b + W1 * x_data2 + W2 * x_data3 # my polinomial model
loss = tf.reduce_mean(tf.square(y - y_data)) # the loss function 
optimizer = tf.train.GradientDescentOptimizer(0.5) # the optimizer
train = optimizer.minimize(loss)  #the train function
init = tf.initialize_all_variables() #variable initialization
sess = tf.Session() #tf session
sess.run(init) #initialization of variables

for step in xrange(16): #I train for only 16 times
    sess.run(train) #execute the gradient decent optimizer once.
    plt.plot(x_data, y_data, 'ro') #plot the original data
    plt.plot(x_data, sess.run(W) * x_data + sess.run(b) + sess.run(W1) * x_data2 + sess.run(W2) * x_data3,'ro') #plot my model
    plt.xlabel('x')             
    plt.xlim(-8,6)
    plt.ylim(-8,6)
    plt.ylabel('y')
    plt.legend()
    plt.show()
    print(sess.run(loss))

sess.close()

答案 2 :(得分:1)

仅在第一个输入字段的keyup上触发事件。将类似的keyup事件添加到第二个字段,它将起作用