小编eek*_*eek的帖子

在jquery插件中获取初始选择器

我之前得到了一些关于选择器的帮助,但我坚持以下.

假设你有一个像这样的插件

$('#box').customplugin();
Run Code Online (Sandbox Code Playgroud)

如何将#box作为插件中的字符串?不确定这是否是正确的做法,任何其他解决方案也会很棒.

考虑#box是一个选择下拉列表,

我遇到的问题是如果我做常规的JavaScript

$('#box').val(x);
Run Code Online (Sandbox Code Playgroud)

选择了正确的选项值,

但如果我在插件中尝试相同的内容

.....
this.each(function() {
var $this = $(this);


$this.val(x);
Run Code Online (Sandbox Code Playgroud)

最后一个代码并没有真正做任何事情.

我注意到我在插件中定位#box时遇到了麻烦,因为它是一个对象,而不是一个字符串......

任何帮助,将不胜感激.

谢谢!

编辑::放入我正在努力的代码以便更好地理解

(function($){
$.fn.customSelect = function(options) {
    var defaults = {
        myClass : 'mySelect'
    };
    var settings = $.extend({}, defaults, options);


    this.each(function() {
        // Var          
        var $this = $(this);
        var thisOpts = $('option',$this);
        var thisSelected = $this[0].selectedIndex;
        var options_clone = '';

        $this.hide();

        options_clone += '<li rel=""><span>'+thisOpts[thisSelected].text+'</span><ul>'
        for (var index in thisOpts) {
            //Check to see if option has any text, …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

19
推荐指数
2
解决办法
1万
查看次数

通过ajax显示JSON数组(jquery)

我是Ajax和JSON的新手,并试图让它工作,但似乎无法掌握它.

如何在ajax中调用json并显示json文件中的所有信息?

这是我的json文件

{ posts: [{"image":"images/bbtv.jpg", "alter":"BioBusiness.TV", "desc":"BioBusiness.TV", "website":"http://andybudd.com/"}, {"image":"images/grow.jpg", "alter":"Grow Staffing", "desc":"Grow Staffing", "website":"http://growstaffing.com/"}]}
Run Code Online (Sandbox Code Playgroud)

和使用的ajax函数

$.ajax({
       type: "GET",
       url: "category/all.js",
       dataType: "json",
       cache: false,
       contentType: "application/json",
       success: function(data) {

            $.each(data.posts, function(i,post){
                            $('#folio').html('<ul><li><div class="boxgrid captionfull"><img src="' + post.image + '" alt="' + post.alter + '" /><div class="cover boxcaption"><p>' + post.desc + '</p><a href="' + post.website + '" target="_blank">More Work</a></div></div></li></ul>');

                    });

            initBinding();
       },
       error: function(xhr, status, error) {
         alert(xhr.status);
       }
    });
Run Code Online (Sandbox Code Playgroud)

出于某种原因,它只显示最后一项....

任何正确方向的帮助都会很棒.

谢谢!

arrays ajax jquery json

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

jquery ×2

ajax ×1

arrays ×1

javascript ×1

json ×1