为什么jquery函数只能在onchange上运行一次?

时间:2016-12-14 06:42:09

标签: javascript jquery select live dropdown

这个想法:每当我更改select选项时,它会检查我的data-x属性是否为==" y",如果是,则警告窗口显示与此相关的数据信息。 这段代码很有效,直到我尝试将这个数据信息打印到名为" result"的div中。在此之后,它只显示一次结果,直到我刷新浏览器,然后一切都恢复正常。我该怎么做呢?我做错了什么?



$(window).on('load', function() {
  $('select').change(function() {
    if ($(this).children('option:selected').data('x') == 'y') {
      alert($(this).children('option:selected').data('info'));
    }
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select required name="client_id" id="client_id" class="form-control selectpicker show-tick form-control" data-live-search="true" data-show-subtext="true">
  <option value="">-- NONE --</option>
  <option data-info="jUST tESTING" data-x="y" value="2" data-subtext="Santa Maria #1234">Antonio Perez</option>
  <option value="5" data-subtext="AV. Los Cerrillos 602, Chile - Santiago">Tatiana Gutierrez</option>
  <option data-info="aNOTHER tEST" data-x="y" value="6" data-subtext="xxxxxxxxxxxxxxxx">Ventas</option>
</select>
&#13;
&#13;
&#13;

谢谢!

2 个答案:

答案 0 :(得分:2)

您需要将data-info的{​​{1}}的值分配给selected option

div

Here是jsfiddle

答案 1 :(得分:0)

您需要将负载更改为就绪:

-(void)viewDidLoad {
    [super viewDidLoad];

    menuArr = [[NSMutableArray alloc]init];

    self.view.backgroundColor = [UIColor whiteColor];

    NSURL *url =[NSURL URLWithString:@"http://url"];

    dataTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data,NSURLResponse *response,NSError *error)
    {
        if(error == nil)
        {
            NSError *JSONError = nil;

            NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&JSONError];

            NSLog(@"Dictionary is:%@",dictionary);
        }
    }];

    [dataTask resume];
}

#pragma mark - NSURLSession delegate methods

- (BOOL)URLSession:(NSURLSession *)session dataTask:(NSURLSessionTask *)dataTask canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace*)protectionSpace
{
    BOOL isvalue = [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];

    return isvalue;
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition,NSURLCredential *credential))completionHandler
{
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)aresponse
{
    receivedData = [[NSMutableData alloc]init];
    [receivedData setLength:0];
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"Received String %@",str);
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionTask *)dataTask connectionDidFinishLoading:(NSURLConnection *)conn
{
    NSError *error;

    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];

    NSString *str=[dict objectForKey:@"Status"];

    NSLog(@"Status Response is:%@",str);

    NSDictionary *jsonDict = [dict valueForKey:@"data"];

    NSLog(@"Json Dictionary is:%@",jsonDict);

    menuArr = [jsonDict valueForKey:@"Text"];

    NSLog(@"Item Name is:%@",menuArr);
}

});

$('select').change(function(){
   if ($(this).children('option:selected').data('x') == 'y') { 
    alert($(this).children('option:selected').data('info'));
  }
 });                     
$(document).ready(function() {
  $('select').change(function() {
    if ($(this).children('option:selected').data('x') == 'y') {
      alert($(this).children('option:selected').data('info'));
    }
  });
});